package com.uenpay.im.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aswife.ui.MaskImageView;
import com.qiyunxin.android.http.adapter.QiYunXinAdapter;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.protocol.QyxTopMsg;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class SelectedTalkListAdapter extends QiYunXinAdapter {
	private LayoutInflater inflater;
	private ArrayList<SelectedQyxTopMsg> data = new ArrayList<SelectedQyxTopMsg>();
	private QyxTopMsg _selectedTalk;

	public SelectedTalkListAdapter(Activity activity) {
		inflater = LayoutInflater.from(activity);
	}

	public void setData(ArrayList<QyxTopMsg> talkEntityArrayList) {
		data = new ArrayList<SelectedQyxTopMsg>();
		for (QyxTopMsg talkEntity : talkEntityArrayList) {
			data.add(new SelectedQyxTopMsg(talkEntity));
		}
		notifyDataSetChanged();
	}

	public void setSelectedItem(int position) {
		for (SelectedQyxTopMsg selectedQyxTopMsg : data) {
			selectedQyxTopMsg.isSelected = false;
		}
		data.get(position).isSelected = true;
		_selectedTalk = data.get(position).talkEntity;
		notifyDataSetChanged();
	}

	public void setSelectedItem(QyxTopMsg topMsg) {
		for (SelectedQyxTopMsg selectedQyxTopMsg : data) {
			if (selectedQyxTopMsg.talkEntity.sessionModel.getSessionType() == topMsg.sessionModel
					.getSessionType()
					&& selectedQyxTopMsg.talkEntity.from_cust_id == topMsg.from_cust_id) {
				selectedQyxTopMsg.isSelected = true;
				_selectedTalk = selectedQyxTopMsg.talkEntity;
			} else {
				selectedQyxTopMsg.isSelected = false;
			}
		}
		notifyDataSetChanged();
	}

	public QyxTopMsg getSelectedItem() {
		return _selectedTalk;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public SelectedQyxTopMsg getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.pick_talk_item, parent,
					false);
			holder.image = (MaskImageView) convertView
					.findViewById(R.id.talk_image);
			holder.selectedMark = (ImageView) convertView
					.findViewById(R.id.talk_selected_mark);
			holder.textView = (TextView) convertView
					.findViewById(R.id.talk_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SelectedQyxTopMsg selectedQyxTopMsg = getItem(position);

		QyxTopMsg topMsg = selectedQyxTopMsg.talkEntity;
		String from_icon = "";
		if (topMsg.sessionModel.getSessionType() == PacketType.GROUP) {
			from_icon = APIConfiguration.getAvatarUrlNormal(
					topMsg.sessionModel.getSessionId() + "", PacketType.GROUP);
		} else {
			from_icon = APIConfiguration.getAvatarUrlNormal(
					topMsg.sessionModel.getSessionId() + "",
					PacketType.PERSONAL);
		}
		holder.image.SetUrl(from_icon);
		holder.textView.setText(topMsg.chat_name);
		if (selectedQyxTopMsg.isSelected) {
			holder.selectedMark.setImageResource(R.drawable.icon_checked);
		} else {
			holder.selectedMark.setImageResource(R.drawable.icon_uncheck);
		}

		return convertView;
	}

	class ViewHolder {
		ImageView selectedMark;
		MaskImageView image;
		TextView textView;
	}

	class SelectedQyxTopMsg {
		public QyxTopMsg talkEntity;
		public boolean isSelected;

		public SelectedQyxTopMsg(QyxTopMsg talkEntity) {
			this.talkEntity = talkEntity;
			this.isSelected = false;
		}

		public void setSelected(boolean isSelected) {
			this.isSelected = isSelected;
		}
	}
}
